package com.xhs.main.controller.rsb;

import com.alibaba.fastjson.JSONObject;
import com.xhs.main.entity.rsb.RsbLayui;
import com.xhs.main.entity.rsb.RsbMoney;
import com.xhs.main.service.rsb.impl.RsbMoneyServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author author
 * @create 2020-10-22-20:48
 */
@Controller
public class RsbMoneyController {

    @Autowired
    RsbMoneyServiceImpl rsbMoneyService;

    //提交已完成的添加修改工资项
    @ResponseBody
    @RequestMapping("/rsb/money")
    public void insertRsbMoney(RsbMoney rsbMoney){

        RsbMoney rsbMoney1 = rsbMoneyService.selectRsbMoneyByNum(rsbMoney.getRsbMonNum());
        if(rsbMoney1==null){
            //添加
            rsbMoneyService.insertRsbMoney(rsbMoney);
        }else{
            //修改
            rsbMoneyService.updateRsbMoney(rsbMoney,rsbMoney.getRsbMonDep(),rsbMoney.getRsbMonPost());
        }
    }

    //编辑工资项回显
    @ResponseBody
    @RequestMapping("/rsb/updateMoney")
    public RsbMoney showUpdateRsbMoney(String moneyNum){
        RsbMoney rsbMoney = rsbMoneyService.selectRsbMoneyByNum(moneyNum);
        return rsbMoney;
    }

    //删除工资项
    @ResponseBody
    @RequestMapping("/rsb/deleteMoney")
    public void deleteRsbMoney(String rsbMoneyNum){
        List<String> rsbMoneyNums=new ArrayList<>();
        rsbMoneyNums.add(rsbMoneyNum);
        rsbMoneyService.deleteRsbMoney(rsbMoneyNums);
    }
    //展示所有工资项
    @ResponseBody
    @RequestMapping("/rsb/showMoney")
    public RsbLayui showRsbMoney(RsbMoney rsbMoney,int page,int limit){
        //得到总条数
        int size = rsbMoneyService.selectChoose(rsbMoney.getRsbMonDep(), rsbMoney.getRsbMonPost()).size();
        //得到数据
        List<RsbMoney> rsbMonies1 = rsbMoneyService.selectChooseLimit(rsbMoney.getRsbMonDep(), rsbMoney.getRsbMonPost(), (page-1)*limit,limit);
        //返回到页面并展示数据
        return new RsbLayui(0,"",size,rsbMonies1);
    }
    //模糊查询带分页
    @ResponseBody
    @RequestMapping("/rsb/searchMoney")
    public RsbLayui searchRsbMoney(@RequestParam("condition")String condition, int page, int limit){
        //获取前端传递的json数据
        JSONObject object = JSONObject.parseObject(condition);
        String rsbMonDep=(String)object.get("rsbMonDep");
        String rsbMonPost=(String)object.get("rsbMonPost");
        //得到总条数
        int size = rsbMoneyService.selectChoose(rsbMonDep, rsbMonPost).size();
        //得到数据
        List<RsbMoney> rsbMonies1 = rsbMoneyService.selectChooseLimit(rsbMonDep,rsbMonPost, (page-1)*limit,limit);
        //返回到页面并展示数据
        return new RsbLayui(0,"",size,rsbMonies1);
    }
}
